//
//  FirstLaunchViewController.m
//  ConvenientlyTake
//
//  Created by 森鑫 on 2023/1/4.
//

#import "FirstLaunchViewController.h"
#import "LoginVC.h"
#import "TGTabBarVC.h"

@interface FirstLaunchViewController ()<UIScrollViewDelegate>
{
    UIScrollView * _scrollView;
    NSArray * imgs;
//    UIPageControl * _control;
}

@end

@implementation FirstLaunchViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationController.navigationBar.hidden = YES;
    self.view.backgroundColor = [UIColor whiteColor];
    _scrollView = [[UIScrollView alloc]initWithFrame:self.view.bounds];
    _scrollView.pagingEnabled = YES;
    _scrollView.showsHorizontalScrollIndicator = NO;
    _scrollView.showsVerticalScrollIndicator =NO;
    [self.view addSubview:_scrollView];
    
    NSArray * image =@[@"event_bg1",@"event_bg2",@"event_bg3",@"event_bg4"];
    NSArray * titleArr = @[@"用户控制",@"事件处理",@"统计大屏",@""];
    NSArray * tipArray = @[@"用户管理与服务控制中心",@"事件流转及综合处理中心",@"数据统计与事件管制中心",@""];
    for (int i =0; i<image.count; i++) {
        UIImage *bgimg = kImage(@"event_bgImage");
        UIImageView *bgImageView = [[UIImageView alloc] initWithImage:bgimg];
        [_scrollView addSubview:bgImageView];
        [bgImageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(SCREEN_WIDTH*i);
            make.width.mas_equalTo(SCREEN_WIDTH);
        }];
        
        UIImageView * imV = [[UIImageView alloc] init];
        imV.image = kImage(image[i]);
        [_scrollView addSubview:imV];
        if (i==image.count-1) {
            UIButton * btn = [UIButton buttonWithType:UIButtonTypeCustom];
            [btn setTitle:@"欢迎使用" forState:UIControlStateNormal];
            btn.layer.cornerRadius = 2;
            btn.backgroundColor = RGBA(22, 93, 255, 1);
            btn.titleLabel.font = TGAutoFont(16);
            [btn addTarget:self action:@selector(FirstPageVC) forControlEvents:UIControlEventTouchUpInside];
            [_scrollView addSubview:btn];
            [btn mas_makeConstraints:^(MASConstraintMaker *make) {
                make.bottom.equalTo(self.view.mas_bottom).offset(IS_IPHONEX?-146:-90);
                make.centerX.equalTo(bgImageView.mas_centerX);
                make.width.mas_equalTo(SCREEN_WIDTH - 56);
                make.height.mas_equalTo(44);
            }];
            [bgImageView layoutIfNeeded];
            [imV mas_makeConstraints:^(MASConstraintMaker *make) {
                make.top.equalTo(bgImageView.mas_bottom).offset(-bgImageView.frame.size.height / 2);
                make.centerX.mas_equalTo(bgImageView.mas_centerX);
            }];
            UILabel *tipLabel = [[UILabel alloc] init];
            tipLabel.text = @"随手拍";
            tipLabel.textColor = RGBA(59, 66, 81, 1);
            tipLabel.font = [TG_UIHelper TGAutoFount:28 name:Ping_Medium];
            [_scrollView addSubview:tipLabel];
            [tipLabel mas_makeConstraints:^(MASConstraintMaker *make) {
                make.top.equalTo(imV.mas_bottom);
                make.centerX.equalTo(bgImageView.mas_centerX);
            }];
            
        } else {
            [imV mas_makeConstraints:^(MASConstraintMaker *make) {
                make.top.equalTo(bgImageView.mas_bottom).offset(-bgImageView.frame.size.height / 2 + 20);
                make.centerX.mas_equalTo(bgImageView.mas_centerX);
            }];
                          
            UILabel *tipLabel = [[UILabel alloc] init];
            tipLabel.text = [tipArray objectAtIndex:i];
            tipLabel.textColor = RGBA(146, 154, 168, 1);
            tipLabel.font = TGAutoFont(16);
            [_scrollView addSubview:tipLabel];
            [tipLabel mas_makeConstraints:^(MASConstraintMaker *make) {
                make.bottom.equalTo(self.view.mas_bottom).offset(IS_IPHONEX?-146:-90);
                make.centerX.equalTo(bgImageView.mas_centerX);
            }];
            UILabel *titleLabel = [[UILabel alloc] init];
            titleLabel.text = [titleArr objectAtIndex:i];
            titleLabel.textColor = RGBA(59, 66, 81, 1);
            titleLabel.font = TGAutoFont(20);
            [_scrollView addSubview:titleLabel];
            [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
                make.bottom.equalTo(tipLabel.mas_top).offset(-12);
                make.centerX.equalTo(bgImageView.mas_centerX);
            }];
        }
    }
    _scrollView.contentSize = CGSizeMake(image.count*SCREEN_WIDTH, 0);
    _scrollView.delegate = self;
//    _control = [[UIPageControl alloc]initWithFrame:CGRectMake(137.5, SCREEN_WIDTH-50, 100, 20)];
//    _control.numberOfPages = image.count;
//    _control.pageIndicatorTintColor = [UIColor whiteColor];
//    _control.currentPageIndicatorTintColor = [UIColor orangeColor];
//    [self.view addSubview:_control];
}

//  跳转W首页
- (void)FirstPageVC{
    
    LoginVC *loginVC = [[LoginVC alloc] init];
    loginVC.view.frame = [UIScreen mainScreen].bounds;
    loginVC.complentBlock = ^{
//        TGTabBarVC *tabbar = [[TGTabBarVC alloc] init];
//        [self.navigationController pushViewController:tabbar animated:YES];
        AppDelegate *delegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
        delegate.tabarController = [[TGTabBarVC alloc] init];
        [delegate.window setRootViewController:delegate.tabarController];
        [delegate.window makeKeyAndVisible];
    };
    UIViewController *rootVC = [[UIApplication sharedApplication].keyWindow rootViewController];
    [rootVC.view addSubview:loginVC.view];
    [rootVC addChildViewController:loginVC];
}

#pragma mark - UIScrollView 跳转W首页
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    
//    CGPoint offset =  _scrollView.contentOffset;
//    NSInteger index = offset.x/SCREEN_WIDTH;
//    _control.currentPage = index;
//    if (offset.x>3*kWidth) {
//        [self FirstPageVC];
//    }
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

@end

